/*=============================================================================

  SYSTEM MANAGER MODULE

  FILE: SYSmgr.cpp
  
  DESCRIPTION

    Manager Module Definition

  EXTERNALIZED FUNCTIONS

    
  Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

                             EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who     what, where, why
  ---------  -------  -------------------------------------------------------
   06/28/03   nso4th   Version 0.1

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include <Cust_AppDef.h>
#include <SYSmgr.h>

/*=============================================================================
  CONSTANT IMPLEMENTATION
=============================================================================*/
#define STATE_TICK_TIME				(50) // 50msec.

/*=============================================================================
  SYSmgr CLASS IMPLEMENTATION
=============================================================================*/
SYSmgr::SYSmgr():mgrBase()
{
	int32		i;

	// Set the application name
	SetAppName(CUST_APP_NAME_SHORT);
	
	// Retrieve the event
	Event_SetType(SYS_MGR);
	
	// Retrieve the state machine control
	State_SetTickTime(STATE_TICK_TIME);
	State_SetEventID(CHANGE_MAJSTATE_E, CHANGE_MINSTATE_E);

	m_mjPrev = NONE_S;
	memZeros(&m_mnPrev[0], sizeof(m_mnPrev));

	// Register the timer ID
	for(i=0; i<MGR_MAX_TIMER; i++)
	{
		Timer_SetEvent(i, (TIMER_0_E+i));
		Timer_Dereg(i);
	}
	MON_Alloc();
	
	// Application
	APP_Alloc();
}

SYSmgr::~SYSmgr()
{
	// Application
	APP_Free();

	MON_Free();
}

boolean SYSmgr::Init(void)
{
	ObjList_Set(SYS_MGR, this);
	
	Event_Handler(SYS_MGR, INIT_START_E, 0, 0);

	mgrBase::Init();

	MON_Init();
	MGR_Init();

	STATE_UNLOCK();
	State_Change(IDL_S, IDL_IDL_S);
	
	Event_Handler(SYS_MGR, INIT_DONE_E, 0, 0);

	return	TRUE;
}

boolean SYSmgr::Close(void)
{
	Event_Handler(SYS_MGR, CLOSE_START_E, 0, 0);
	ObjList_Set(SYS_MGR, NULL);

	// Kill the state timer
	STATE_LOCK();
	if(m_tickTimer)
	{
		Timer_Kill(m_tickTimer);
		Timer_Free(m_tickTimer);
		m_tickTimer = NULL;
	}
	Event_SetRecv(NULL);
	
	MON_Close();
	MGR_Close();

	mgrBase::Close();

	Event_Handler(SYS_MGR, CLOSE_DONE_E, 0, 0);
	
	return	TRUE;
}

boolean SYSmgr::Stop(void)
{
	dword		mj_sts;

	mj_sts = State_MajCurrent();

	if((mj_sts == DEV_S) || (mj_sts == IDL_S))
	{
		return	TRUE;
	}

	State_SendCommand(STATE_STOP_C, 0, 0);
	SYS_DELAY(10);
	
	if(State_MajCurrent() != IDL_S)  // once again
	{
		State_SendCommand(STATE_STOP_C, 0, 0);
		SYS_DELAY(10);
	}
	
	return	TRUE;
}


/*--- END OF SYSmgr.CPP ---*/
